package sdcncsi.ict.counter;

import java.util.LinkedHashMap;
import java.util.Map.Entry;

public class LRUMapCache<K, V> extends LinkedHashMap<K, V> {
	private static final long serialVersionUID = 1L;
	private int maxSize = 16;

	/**
	 * 设定LRUMapCache的最大容量(默认设定accessOrder=true)
	 * 
	 * @param maxSize
	 * @see java.util.LinkedHashMap#LinkedHashMap(int initialCapacity, float
	 *      loadFactor, boolean accessOrder)
	 */
	public LRUMapCache(int maxSize) {
		super(maxSize, 0.75f, true);
		this.maxSize = maxSize;
	}
	
	/**
	 * 构造方法二，允许自定以accessOrder值
	 * 
	 * @param maxSize
	 * @param accessOrder
	 */
	public LRUMapCache(int maxSize, boolean accessOrder) {
		super(maxSize, 0.75f, accessOrder);
		this.maxSize = maxSize;
	}

	/**
	 * 获得LRUMapCache的最大容量值
	 * @return
	 */
	public int getMaxSize(){
		return this.maxSize;
	}
	
	/**
	 * 是否删除元素判定方法
	 * 
	 * @see java.util.LinkedHashMap#removeEldestEntry(java.util.Map.Entry)
	 */
	protected boolean removeEldestEntry(Entry<K, V> eldest) {
		return (size() > this.maxSize);
	}

}
